<template>
  <div class="modify">
    <div class="cropper-box" v-if="showCropper">
      <vue-cropper ref="cropper" v-on:closeCropper="cropperData"></vue-cropper>
    </div>
    <ul class="user-info">
      <li class="first-li" v-show="level>2">
        <div class="li-left">
          <span class="first-li">主图</span>
        </div>
        <div class="li-right" @click="choseImg(1)">

          <img v-if="imgPath==''||imgPath==null" src="../../assets/images/add_icon.png" alt="">
          <img v-else="imgPath!=''" :src="imgPath" alt="">
          <!--<div class="input"   @click="choseImg()"></div>-->
        </div>
      </li>
      <li>
        <div class="li-left">
          <span>昵称</span>
        </div>
        <div class="li-right">
          <input type="text" v-model="nickname" placeholder="请输入"/>
        </div>
      </li>
      <li>
        <div class="li-left">
          <span>性别</span>
        </div>
        <div class="li-right" v-if="isIos" @click="changeGender()">
          <select v-model="selectedGender">
            <option value="1" >男</option>
            <option value="2" >女</option>
          </select>
        </div>
        <div class="li-right" v-if="!isIos">
          <select v-model="selectedGender">
            <option value="1" >男</option>
            <option value="2" >女</option>
          </select>
        </div>
      </li>
      <li>
        <div class="li-left">
          <span >年龄</span>
        </div>
        <div class="li-right">
          <!--<input type="text" v-model="age" placeholder="请输入"/>-->
          <select name="" v-model="selectedAge">
            <option :value="item" v-for="(item ) in ages">{{item}}岁</option>
          </select>
        </div>
      </li>
      <li>
        <div class="li-left">
          <span>身高</span>
        </div>
        <div class="li-right">
          <!--<input type="text" v-model="weight" placeholder="请输入"/>-->
          <select name="" v-model="selectedStature" >
            <option :value="item" v-for="(item) in stature">{{item}}cm</option>
          </select>
        </div>
      </li>
      <li>
        <div class="li-left">
          <span>学历</span>
        </div>
        <div class="li-right">
          <select v-model="qualifications">
            <option value="博士">博士</option>
            <option value="研究生">研究生</option>
            <option value="本科">本科</option>
            <option value="大专">大专</option>
            <option value="高中">高中</option>
          </select>
        </div>
      </li>

      <li>
        <div class="li-left">
          <span>籍贯</span>
        </div>
        <div class="li-right">
          <!--<input type="text" v-model="nativePlace" placeholder="请输入"/>-->
          <select name="" v-model="nativePlace" >
            <option :value="item" v-for="(item) in province">{{item}}</option>
          </select>
        </div>
      </li>
      <!--<li>-->
      <!--<div class="li-left">-->
      <!--<span>地址</span>-->
      <!--</div>-->
      <!--<div class="li-right" @click="addrPop()">-->
      <!--<input class="isDisabled" type="text" v-model="address" placeholder="请选择" disabled="disabled"/>-->
      <!--</div>-->
      <!--</li>-->
      <!--<li>-->
      <!--<div class="li-left">-->
      <!--<span>详细地址</span>-->
      <!--</div>-->
      <!--<div class="li-right">-->
      <!--<input type="text" v-model="addressDetails" placeholder="请输入"/>-->
      <!--</div>-->
      <!--</li>-->
      <li class="li-left first-li" style="position: relative" v-show="level>2">
        <div class="li-left">
          <span>详情图</span>
        </div>
        <div class="li-right" @click="choseImg(2)">
          <img src="../../assets/images/add_icon.png" alt="">
          <!--<input type="file" @change="choseImg1($event)"/>-->
        </div>
        <!--<div style="width: 50px; position:absolute;top: -20px;right: 20px;">-->
        <!--<div class="btn">上传</div>-->

        <!--</div>-->
      </li>
      <li class="li-left" v-show="level>2">
        <div class="li-right">
          <span v-for="(item,index) in imgList">
            <img :src="item.imgUrl" alt="" @click="removeImg(index)">
          </span>
        </div>
      </li>
      <li class="last-li">
        <textarea maxlength="120" v-model="introduceSelf" placeholder="自我介绍(不超过120个字)"></textarea>
      </li>
    </ul>
    <p class="tip">*注：请填写真实信息</p>
    <div class="btn" @click="confirmBtn()">确定</div>
    <!--地址选择弹窗-->
    <div class="addr-pop" v-if="showAddr" :class="{popToggle:showAddr}">
      <div style="width:100%;height:100%;" @click="showAddr=false;"></div>
      <div class="ap-bg">
        <div class="confrim-btn"><span @click="confirmPopBtn()">确定</span></div>
        <city-picker></city-picker>
      </div>
    </div>

  </div>

</template>

<script>
  import VueCropper from '../public/vueCropper.vue'
  import global from '../public/global.vue'
  export default {
    name: 'name',
    components: {
      'vue-cropper':VueCropper
    },
    data() {
      return {
        selectedGender: '',//性别选择
        imgPath: "",//图片路径
        nickname: '',//昵称
        ages: [],//年龄段
        selectedAge:18,//选择的年龄  默认18
        stature: [],//身高
        selectedStature:172,//选择的身高 默认172
        qualifications: '高中',//学历 默认 高中
        introduceSelf: '',//自我介绍
        showAddr: false,//显示地址弹窗
        openPop: false,//删除弹窗
        address: "请选择",//选择后的地址
        addressDetails: "",
        level: 1,
        imgList: [],//详情图列表
        imgUrl: global.sourceUrl,//图片路径
        //------cropper start-----
        showCropper:false,//显示裁剪窗
        //------cropper end-----
        imgupload:0,//图片是否为新上传
        whoClick:1,
        province:global.province,//籍贯
        nativePlace:'北京',//选择的籍贯 默认北京
        isIos:'',
      }
    },
    created: function () {
      document.title = "修改信息"
      // --------初始化数据 start-------------
      for(let i=150;i<=200;i++){
        this.stature.push(i) //初始化身高
      }
      for(let i=18;i<=50;i++){
        this.ages.push(i); //初始化年龄
      }
      // --------初始化数据 end-------------
      //获取信息
      let url = global.apiUrl;
      let self = this;
      self.level = global.getCookie("level");

      self.$http.get(url + '/user/getUserInfo').then(res => {
        self.nickname = res.data.data.userInfo.nickname;
        if (res.data.data.realInfo != null) {
          let realInfo = res.data.data.realInfo;
          self.selectedGender = realInfo.sex;
          self.selectedAge = realInfo.age;//年龄
          self.selectedStature = realInfo.height;//身高
          self.qualifications = realInfo.educate;//学历
          self.nativePlace = realInfo.birth;//籍贯
          self.introduceSelf = realInfo.selfInfo;
          //自我
          //self.level=realInfo.level;
          self.imgPath = self.imgUrl+realInfo.imageUrl;
          self.imgList = res.data.data.realImg;
          for (let i = 0; i < res.data.data.realImg.length; i++) {

            self.imgList[i].imgUrl = self.imgUrl + res.data.data.realImg[i].imgUrl;
          }


          let s = realInfo.address;
          s = s.split(";");
          self.address = s[0];
          self.addressDetails = s[1];
        }

      })
    },
    mounted() {
      let u=navigator.userAgent;
      let isIos=!!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
      if(isIos){
        this.isIos=isIos;
      }else{
        this.isIos=false;
      }

    },
    methods: {
      changeGender(){
        this.selectedGender=1;
      },

      //图片删除
      removeImg(idx){
        if(window.confirm('你确定要删除么？')){
          if(this.imgList[idx].id==0){
            this.imgList.splice(idx,1);
          }else{
            this.$http.get(global.apiUrl+"/user/delImgs?id="+this.imgList[idx].id).then(res => {
              this.imgList.splice(idx,1);
            })
          }
        }

      },
      //-----------croppper start----------------
      cropperData(e){
        this.showCropper=e;
        if(this.whoClick==1){
          this.imgPath=this.$refs.cropper.downImg;
          this.imgupload=1;
        }else{
          let obj = new Object();
          obj.imgUrl = this.$refs.cropper.downImg;
          obj.id = 0;
          this.imgList.push(obj);

        }


      },
      //-----------croppper end------------------
      //地址弹窗
      addrPop() {
        this.showAddr = true;
      },
      //确定 所选地址
      confirmPopBtn() {
        let data = this.$children[0]._data;
        this.showAddr = false;
        this.address = data.selectProvince + data.selectCity + data.selectCounty;
      },
      //确定
      confirmBtn() {

        //上传轮播图
        let self = this;


        for (let i = 0; i < self.imgList.length; i++) {
          if (self.imgList[i].id == 0) {
            let param = new URLSearchParams();
            param.append("imgUrl", self.imgList[i].imgUrl);
            self.$http({
              method: "post",
              url: global.apiUrl + '/user/uploadRealImg',
              data: param,
            }).then(res => {
              if (res.data.code == 0) {

              } else {
                alert(res.data.msg)
              }
            })
          }
        }

        if(self.imgupload==1){

          let param = new URLSearchParams();
          param.append("imgUrl", self.imgPath);
          self.$http({
            method: "post",
            url: global.apiUrl + '/user/updateImg',
            data: param,
          }).then(res => {
            if (res.data.code == 0) {


              self.imgPath =res.data.data;
              let user=new Object();
              user.nickname=self.nickname;
              let real=new Object();

              real.sex=self.selectedGender;
              real.age=self.selectedAge;
              real.height=self.selectedStature;
              real.educate=self.qualifications;
              real.birth=self.nativePlace;
              real.selfInfo=self.introduceSelf;
              real.imageUrl=self.imgPath;
              real.address=self.address;
              let params = new URLSearchParams();
              params.append("user",JSON.stringify(user));
              params.append("real",JSON.stringify(real));
              self.$http({
                url: global.apiUrl + '/user/updateUser',
                method: 'post',
                data: params,
              }).then(res => {
                if(res.data.code==0){
                  alert("修改成功");
                  window.localStorage.setItem("username",self.nickname);
                  setTimeout(function () {
                    self.$router.go(-1)
                  }, 1000 )
                  //window.localStorage.setItem("headImg",self.headImg);
                }
              })



            } else {
              alert(res.data.msg)
            }
          })


        }else{

          let user=new Object();
          user.nickname=self.nickname;
          let real=new Object();
          real.sex=self.selectedGender;
          real.age=self.selectedAge;
          real.height=self.selectedStature;
          real.educate=self.qualifications;
          real.birth=self.nativePlace;
          real.selfInfo=self.introduceSelf;
          real.imageUrl=self.imgPath;
          real.address=self.address;
          let params = new URLSearchParams();
          params.append("user",JSON.stringify(user));
          params.append("real",JSON.stringify(real));
          self.$http({
            url: global.apiUrl + '/user/updateUser',
            method: 'post',
            data: params,
          }).then(res => {
            if(res.data.code==0){
              alert("修改成功");
              window.localStorage.setItem("username",self.nickname);
              setTimeout(function () {
                self.$router.go(-1)
              }, 1500 )
              //window.localStorage.setItem("headImg",self.headImg);
            }
          })

        }






      },
      //头像上传
      choseImg(num) {
        if(num==2&&this.imgList.length>=5){
          alert("已达上限")
          return;
        }
        let that = this;
        that.whoClick=num;
//        let file = e.target.files[0];
//        let imgSize = file.size / 1024;
        that.showCropper=true;
//        that.imgPath = e.target.value;
//        if (imgSize > 5000) {
//          alert('请上传大小不要超过5M的图片')
//        } else {
//          let reader = new FileReader();
//          reader.readAsDataURL(file); // 读出 base64
//          reader.onloadend = function () {
//            // 图片的 base64 格式, 可以直接当成 img 的 src 属性值
//            let dataURL = reader.result;
//            that.imgPath = reader.result;
//            let param = new URLSearchParams();
//            param.append("imgUrl", that.imgPath);
//            that.$http({
//              method: "post",
//              url: global.apiUrl + '/user/updateImg',
//              data: param,
//            }).then(res => {
//              if (res.data.code == 0) {
//                that.imgPath = res.data.data;
//              } else {
//                alert(res.data.msg)
//              }
//            })
//          };
//        }

      },
      //详情图上传
      choseImg1(e) {
        let that = this;
        if (that.imgList.length >= 5) {
          alert("已达上限");
          return;
        }
        let file = e.target.files[0];
        let imgSize = file.size / 1024;
        if (imgSize > 2000) {
          alert('请上传大小不要超过2M的图片')
        } else {
          let reader = new FileReader();
          reader.readAsDataURL(file); // 读出 base64
          reader.onloadend = function () {
            // 图片的 base64 格式, 可以直接当成 img 的 src 属性值
            let dataURL = reader.result;
            let obj = new Object();
            obj.imgUrl = dataURL;
            obj.id = 0;
            that.imgList.push(obj);
          };
        }

      },

    },
    computed:{

    }

  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="less" scoped>
  .modify {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    min-height: 100%;
    background: #f3f3f3;
    .user-info {
      width: 100%;
      padding: 0 2.5%;
      background: #fff;
      li {
        width: 100%;
        padding: 2% 0;
        font-size: 0.9rem;
        display: flex;
        border-bottom: 1px solid #eee;
        .li-left {
          flex: 1;
          span {
            color: #666;
            vertical-align: middle;
          }
          .first-li {
            line-height: 50px;
          }
        }
        .li-right {
          flex: 3;
          img {
            width: 50px;
            height: 50px;
            /*border-radius:50%;*/
            vertical-align: middle;
          }
          .input {
            -webkit-appearance: none;
            width: 100%;
            border: none;
            outline: none;
            font-size: 0.9rem;
          }
          input {
            -webkit-appearance: none;
            width: 100%;
            border: none;
            outline: none;
            font-size: 0.9rem;
          }
          select {
            -webkit-appearance: none;
            width: 100%;
            border: none;
            outline: none;
            background: transparent;
            font-size: 0.9rem;
          }
        }
      }
      .first-li {
        .li-right {
          position: relative;
          input {
            position: absolute;
            top: 0;
            left: 0;
            width: 50px;
            height: 50px;
            opacity: 0;
            cursor: pointer;
          }
        }
      }
      .last-li {
        border-color: transparent;
        textarea {
          width: 100%;
          padding: 0 1%;
          resize: none;
          border: none;
          outline: none;
        }
        textarea::-webkit-input-placeholder {
          color: #ddd;
          font-size: 0.8rem;
        }
      ;
      }
    }
    /*注*/
    .tip {
      width: 100%;
      height: 1.5rem;
      line-height: 1.5rem;
      padding: 0 3%;
      font-size: 0.6rem;
      color: #aaa;
    }
    /*确定*/
    .btn {
      width: 90%;
      height: 2rem;
      line-height: 2rem;
      margin: 30px auto;
      text-align: center;
      font-size: 1rem;
      color: #fff;
      border-radius: 6px;
      background: #d44242;
    }
    /*地址弹窗*/
    .addr-pop {
      position: absolute;
      top: 100%;
      left: 0;
      background: transparent;
      padding: 0 1%;
      width: 100%;
      height: 100%;
      transition: all .5s;
      .ap-bg {
        position: absolute;
        bottom: 0;
        left: 0;
        width: 100%;
        height: 410px;
        background: #eee;
      }
      .confrim-btn {
        width: 100%;
        padding: 2% 2.5%;
        text-align: right;
        span {
          font-size: 0.9rem;
          border: 1px solid #09BB07;
          color: #09BB07;
          padding: 3px 2%;
          border-radius: 5px;
        }
      }

    }
    .popToggle {
      top: 0;
    }
    .isDisabled[disabled='disabled'] {
      -webkit-appearance: none;
      background: #fff;
      color: #333;
      opacity: 1;
    }
  }

  /*-------cropper start-------*/
  .cropper-box{
    position: absolute;
    top:0;
    left:0;
    width:100%;
    height:100%;
    background: #000;
    overflow: hidden;
    z-index: 100;
  }
  /*-------cropper end-------*/
</style>
